/*
*  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
*  reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*  1. Redistributions of source code must retain the above copyright
*  notice, this list of conditions and the following disclaimer.
*
*  2. Redistributions in binary form must reproduce the above copyright
*  notice, this list of conditions and the following disclaimer in
*  the documentation and/or other materials provided with the
*  distribution.
*
*  3. The end-user documentation included with the redistribution,
*  if any, must include the following acknowledgment:
*  "This product includes software developed by the
*  Sun Microsystems, Inc. for Project JXTA."
*  Alternately, this acknowledgment may appear in the software itself,
*  if and wherever such third-party acknowledgments normally appear.
*
*  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
*  must not be used to endorse or promote products derived from this
*  software without prior written permission. For written
*  permission, please contact Project JXTA at http://www.jxta.org.
*
*  5. Products derived from this software may not be called "JXTA",
*  nor may "JXTA" appear in their name, without prior written
*  permission of Sun.
*
*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
*  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
*  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
*  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
*  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
*  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*  SUCH DAMAGE.
*  ====================================================================
*
*  This software consists of voluntary contributions made by many
*  individuals on behalf of Project JXTA.  For more
*  information on Project JXTA, please see
*  <http://www.jxta.org/>.
*
*  This license is based on the BSD license adopted by the Apache Foundation.
*
*  $Id: Authenticator.java,v 1.3 2006/02/15 23:46:49 gonzo Exp $
*/

package net.jxta.myjxta.misc.unused;

import net.jxta.ext.config.ResourceManager;
import net.jxta.myjxta.util.Prefs;

/**
 *
 * @version $Id: Authenticator.java,v 1.3 2006/02/15 23:46:49 gonzo Exp $
 *
 * @author james todd [gonzo at jxta dot org]
 */

public abstract class Authenticator {

    protected static final String APPLICATION_AUTHENTICATOR =
        Config.APPLICATION + "/authenticator";
    protected static final String APPLICATION_AUTHENTICATOR_CLASS =
        APPLICATION_AUTHENTICATOR + "/@class";
    protected static final String APPLICATION_AUTHENTICATOR_CLASS_DEFAULT =
        "net.jxta.myjxta.util.LDAPAuthenticator";
    protected static final String APPLICATION_AUTHENTICATOR_IS_VERBOSE =
        APPLICATION_AUTHENTICATOR + "/@isVerbose";

    private static final String CLASS_RULE = "class";
    private static final String VERBOSE_RULE = "verbose";
    private static final Object LOCK = new Object();

    private static Authenticator authenticator = null;

    private String name = null;

    public static void configure()
    throws IllegalArgumentException {
        final String notInstantiatable = "not instantiatable";
        String clazz = Prefs.Default.AUTHENTICATOR_CLASS;

        ResourceManager resources = ResourceManager.getInstance();

        if (clazz == null) {
            clazz = resources.get(APPLICATION_AUTHENTICATOR_CLASS,
                                  APPLICATION_AUTHENTICATOR_CLASS_DEFAULT);
        }

        if (clazz == null) {
            final String nullAuthenticator = "null authenticator";
            throw new IllegalArgumentException(nullAuthenticator);
        }

        synchronized (LOCK) {
            try {
                authenticator = create(clazz);
            } catch (Exception e) {
                throw new IllegalArgumentException(notInstantiatable +
                                                   ": " + clazz);
            }
        }

        authenticator.process(resources);
    }

    public static Authenticator factory(boolean newInstance)
    throws IllegalStateException {
        Authenticator a = null;

        if (authenticator == null) {
            try {
                configure();
            } catch (IllegalArgumentException iae) {}
        }

        if (authenticator != null &&
            ! newInstance) {
            a = authenticator;
        } else if (authenticator != null) {
            final String notInstantiatable = "not instantiatable";
            String clazz = authenticator.getName();

            synchronized (LOCK) {
                try {
                    a = authenticator = create(clazz);
                } catch (Exception e) {}
            }
        }
        else {
            final String msg = "factory error: authenticator is null";

            throw new IllegalStateException(msg);
        }

        return a;
    }

    public abstract String getPrincipal();

    public abstract String getId();

    public abstract String getId(String principal);

    public abstract boolean authenticate(String credentials);

    public abstract boolean authenticate(String principal,
                                         String credentials);

    protected static boolean getIsVerbose() {

        return ResourceManager.getInstance().
               getBoolean(APPLICATION_AUTHENTICATOR_IS_VERBOSE,
                   Prefs.Default.AUTHENTICATOR_VERBOSE);
    }

    protected Authenticator() {
    }

    protected String getName() {
        return this.name;
    }

    protected void process(ResourceManager resources)
    throws IllegalArgumentException {
        this.name =
            resources.get(APPLICATION_AUTHENTICATOR_CLASS,
                Prefs.Default.AUTHENTICATOR_CLASS);

        if (this.name == null) {
            String msg = "bad configuration: " + "\n  name : " + this.name;
            IllegalArgumentException iae = new IllegalArgumentException(msg);

            throw iae;
        }
    }

    private static Authenticator create(String name)
    throws ClassNotFoundException, InstantiationException,
        IllegalAccessException, IllegalArgumentException {
        final String notAssignable = "authenticator is not assignable";
        Class c = Class.forName(name);
        IllegalStateException ise =
            new IllegalStateException(notAssignable + ": " + name);

        if (! Authenticator.class.isAssignableFrom(c)) {
            throw ise;
        }

        return (Authenticator)c.newInstance();
    }
}
